package com.whc.gulimall.cart.controller;

import com.whc.common.utils.R;
import com.whc.gulimall.cart.service.CartService;
import com.whc.gulimall.cart.vo.Cart;
import com.whc.gulimall.cart.vo.CartItem;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @author angaoen
 */
@Controller
public class CartController {

    /**
     * 购物车 Service
     */
    @Resource
    private CartService cartService;

    /**
     * 获取购物车列表
     *
     * @param model 模型
     * @return 跳转
     */
    @GetMapping("/cart.html")
    public String cartListPage(Model model) {
        // 获取购物车列表
        Cart cart = cartService.cartListPage();
        model.addAttribute("cart", cart);
        return "cartList";
    }

    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId, @RequestParam("count") Integer count,
                            RedirectAttributes redirectAttributes) {
        try {
            cartService.addToCart(skuId, count);
            redirectAttributes.addAttribute("skuId", skuId);
        } catch (ExecutionException | InterruptedException e) {
            return "redirect:http://gulimall.com";
        }
        return "redirect:http://cart.gulimall.com/addToCartSuccess.html";
    }

    @GetMapping("/addToCartSuccess.html")
    public String addToCartPage(@RequestParam("skuId") Long skuId, Model model) {
        CartItem cartItem = cartService.getCartItemBySkuId(skuId);
        model.addAttribute("cartItem", cartItem);
        return "success";
    }

    /**
     * 更换商品的选中状态
     *
     * @param skuId   skuId
     * @param checked 1-选中 0-未选中
     * @return 跳转页面
     */
    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId, @RequestParam("checked") Integer checked) {
        cartService.checkItem(skuId, checked);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    /**
     * 更改商品的数量
     *
     * @param skuId skuId
     * @param count 商品数量
     * @return 跳转页面
     */
    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId") Long skuId, @RequestParam("count") Integer count) {
        cartService.countItem(skuId, count);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    /**
     * 商品指定skuId的商品
     *
     * @param skuId skuId
     * @return 跳转页面
     */
    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId) {
        cartService.deleteItem(skuId);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    /**
     * 获取购物车列表
     *
     * @return 统一返回结果
     */
    @GetMapping("/products")
    @ResponseBody
    public R listProductList() {
        return R.ok().setData(cartService.listProductList());
    }
}
